package cn.itcast.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtils {

private static JedisPool jedisPool = null;
static {
    try {
        //读取配置文件
        InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        Properties properties = new Properties();
        properties.load(is);
        //创建配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //设置配置
        jedisPoolConfig.setMaxTotal(Integer.valueOf(properties.getProperty("jedis.MaxTotal")));
        jedisPoolConfig.setMinIdle(Integer.valueOf(properties.getProperty("jedis.MinIdle")));
        jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(properties.getProperty("jedis.MaxWaitMillis")));
        String host=properties.getProperty("jedis.host");
        int port=Integer.valueOf(properties.getProperty("jedis.port"));
        //获取连接池
        jedisPool = new JedisPool(jedisPoolConfig,host,port);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static Jedis getJedis(){
    return jedisPool.getResource();

}

}
